package com.akk.platform.oem.business;

import com.akk.module.avs.entity.AvsYbLoanRecord;
import com.akk.module.avs.entity.AvsYbLoanUser;
import com.akk.module.avs.enums.RecordStatus;
import com.akk.module.avs.model.response.AvsYbLoanRecordRes;
import com.akk.module.avs.service.AvsYbLoanRecordService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.akk.utils.ShiroUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;

import java.util.List;
import java.util.Map;

@Component
public class YbLoanRecordBusiness {

    @Autowired
    private AvsYbLoanRecordService recordService;

    public RestResponse list(Query query, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        Map params = query.getSearch();
        params.put("appId", ShiroUtils.getAdmin().getAppId());
        PageHelper.startPage(query.getPage(),query.getLimit());
        List<AvsYbLoanRecordRes> list = recordService.findByParams(params);
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    public RestResponse recordInfo(Long recordId){
        AvsYbLoanRecord record = recordService.findById(recordId);
        if (record == null)
            return RestResponse.build("1","信息不存在","");
        return RestResponse.build(record);
    }

    public RestResponse userInfo(Long userId){
        AvsYbLoanUser user = recordService.findUserById(userId);
        if (user == null)
            return RestResponse.build("1","信息不存在","");
        return RestResponse.build(user);
    }

    public RestResponse handelRecord(AvsYbLoanRecord record){
        if (record.getStatus() == RecordStatus.REFUSE.getValue() && StringUtils.isEmpty(record.getRemake()))
            return RestResponse.build("1","拒绝原因不能为空",null);
        AvsYbLoanRecord record1 = recordService.findById(record.getRecordId());
        record1.setStatus(record.getStatus());
        record1.setRemake(record.getRemake());
        recordService.update(record1);
        return RestResponse.build("200","success",null);
    }

}
